<template>
  <div style="width: 100%;height: 100vh;background-color: #cccccc ;overflow: hidden">
    <div style="width: 400px;margin: 120px auto">
      <div style="padding: 30px;color: olivedrab;font-size: 30px;text-align: center">
        欢迎登陆
      </div>
      <el-form ref="form" :model="form" size="normal" :rules="rules">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login">登录</el-button>
        </el-form-item>
      
      </el-form>
    </div>
  
  </div>
</template>

<script>
import request from "../utils/request";

export default {
  name: "Login",
  data() {
    return {
      form: {},
      rules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入用户密码', trigger: 'blur'},
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs['form'].validate((valid) => {
            if (valid) {
              request.post("/main/login", this.form).then(res => {
                console.log(res)
                if (res.codes === '200') {
                  this.$message({
                    type: "success",
                    message: "登录成功"
                  })
                  console.log(res.data)
                  sessionStorage.setItem("user", JSON.stringify(res.data)) // 缓存数据
                  this.$router.push("/") //登录成功后跳转主页
                } else {
                  this.$message({
                    type: "error",
                    message: res.msg
                  })
                }
              })
            }
          }
      )
      
    }
  }
}
</script>

<style scoped>

</style>